package cn.wolfcode.luowowo2.articleserver.service.imp;

import cn.wolfcode.luowowo2.article.domain.StrategyTag;
import cn.wolfcode.luowowo2.article.domain.StrategyTagRelation;
import cn.wolfcode.luowowo2.article.mapper.StrategyTagMapper;
import cn.wolfcode.luowowo2.article.mapper.StrategyTagRelationMapper;
import cn.wolfcode.luowowo2.article.service.IStrategyTagService;
import cn.wolfcode.luowowo2.common.query.StrategyTagQueryObject;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created by Albert on 2019/7/9.
 */
@Service
public class StrategyTagServiceImpl implements IStrategyTagService {
    @Autowired
    private StrategyTagMapper mapper;

    @Autowired
    private StrategyTagRelationMapper strategyTagRelationMapper;
    @Override
    public PageInfo<StrategyTag> query(StrategyTagQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<StrategyTag> list = mapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void save(String[] tags, Long id) {
        for (String tag : tags) {
            StrategyTag strategyTag = new StrategyTag();
            strategyTag.setName(tag);
            mapper.insert(strategyTag);

            StrategyTagRelation strategyTagRelation = new StrategyTagRelation();
            strategyTagRelation.setTagId(strategyTag.getId());
            strategyTagRelation.setDetailId(id);
            strategyTagRelationMapper.insert(strategyTagRelation);
        }
    }

    @Override
    public void update(String[] tags, Long id) {
        //先删除关系
        strategyTagRelationMapper.deletByDetailId(id);
        //再重新保存
        save(tags,id);
    }

    @Override
    public List<String> getByDetailId(Long id) {
        return strategyTagRelationMapper.selectByDetailId(id);
    }

    @Override
    public List<StrategyTag> list() {
        return mapper.selectAll();
    }
}
